package com.dk.controller;

import com.dk.em.Result;
import com.dk.page.PageResult;
import com.dk.pojo.Location;
import com.dk.service.LocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("location")
@Api(tags = "库位服务")
@RefreshScope        //开启热更新
public class LocationController {
    
    @Autowired
    private LocationService locationService;

    /*增加ok*/
    @PostMapping
    @ApiOperation("库位增加接口")
    @ApiImplicitParam(name = "库位对象信息",value = "location",required = false)
    public Result insertlocation(@RequestBody Location location){
        locationService.insertlocation(location);
        return Result.OK();
    }


    /*根据id删除ok*/
    @DeleteMapping("deleteById")
    @ApiOperation("根据id删除")
    public Result deletelocation(Integer id){
        locationService.deleteById(id);
        return Result.OK();
    }


    //批量删除ok
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    public Result deleteAlllocation(@RequestBody List<String> ids){
        locationService.deleteAllById(ids);
        return Result.OK();
    }


    /*查询ok*/
    @GetMapping
    @ApiOperation("查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "pageNum", name = "当前页数", required = true),
            @ApiImplicitParam(value = "pageSize", name = "每页条数", required = false)})
    public Result getAlllocation(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        PageResult<Location> pageInfo = locationService.getAlllocation(pageNum,pageSize);
        return Result.OK().setData(pageInfo);
    }

    //模糊查询ok
    @ApiOperation("根据库位名称模糊查询")
    @ApiImplicitParam(name = "库位名称",value = "name",required = true)//必填
    @GetMapping("getlocationByName")
    public Result getlocationByName(String name){
        List<Location> locationList = locationService.getlocationByName(name);
        return Result.OK().setData(locationList);
    }


    //回显根据id查询数据ok
    @ApiImplicitParam(name = "库位id",value = "id",required = true)
    @GetMapping("getById")
    public Result getById(Integer id){
        Location location = locationService.getById(id);
        return Result.OK().setData(location);
    }


    /*修改*/
    @PutMapping
    @ApiOperation("修改库位接口")
    @ApiImplicitParam(name = "库位对象",value = "location",required = false)
    public Result updatelocation(@RequestBody Location location){
        locationService.updatelocation(location);
        return Result.OK();
    }
}
